package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.mapper.FavoriteMapper;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.mapperExt.FavoriteMapperExt;
import com.itheima.travel.mapperExt.RouteMapperExt;
import com.itheima.travel.pojo.Favorite;
import com.itheima.travel.pojo.FavoriteExample;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.req.FavoriteVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.req.UserVo;
import com.itheima.travel.service.FavoriteService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private RouteMapperExt routeExtMapper;
    @Autowired
    private HttpSession session;

    @Autowired
    private FavoriteMapperExt favoriteExtMapper;
    /**3
     * 添加收藏
     * @param favoriteVo
     * @return
     */
    @Override
    public Integer addFavorite(FavoriteVo favoriteVo) {
        // 判断用户是否登录
        UserVo userVo = (UserVo) session.getAttribute(session.getId());
        if(EmptyUtil.isNullOrEmpty(userVo)){
            System.out.println("当前没有用户登录");
            return null;
        }
        // 获取用户的id值
        favoriteVo.setUserId(userVo.getId());
        // 1.添加收藏 -> 向中间表添加一条记录
        Favorite favorite = BeanConv.toBean(favoriteVo, Favorite.class);
        favoriteMapper.insert(favorite);
        // 2.添加成功后,修改线路的收藏次数  +1
        routeExtMapper.updateRouteCountById(favoriteVo.getRouteId());
        // 3.查询最新收藏次数
        Route route = routeMapper.selectByPrimaryKey(favoriteVo.getRouteId());
        return route.getAttentionCount();
    }



    /**
     * 分页查询我的收藏
     *      分页查询插件: mybatis的Pagehelper插件
     *          1.在core模块中导入Pagehelper的jar包
     *          2.在core模块下的mybatis核心配置类中完成插件的配置
     *              a.创建分页查询拦截器类对象
     *              b.通过sqlSessionFactoryBean将创建好的拦截器交个Mybatis调用
     *          3.编写业务
     *      ThreadLocal: 线程对象,一个线程有一个这样的对象
     * @return
     */


    @Override
    public PageInfo<RouteVo> findMyFavorite( int pageNum, int pageSize) {
        // 将当前页和每页展示的条数绑定到当前线程上.
        PageHelper.startPage(pageNum, pageSize);
        //获取当前登录用户
        Object object = session.getAttribute(session.getId());
        UserVo userVo = null;
        if (!EmptyUtil.isNullOrEmpty(object)){
            userVo= (UserVo) object;
        }else {
            return null;
        }
        // 查询当前用户收藏的线路信息
        List<Route> routeList = favoriteExtMapper.findFavoriteByUserId(userVo.getId());
        // ★立即将查询结果封装到PageInfo
        PageInfo<Route> pageInfo = new PageInfo<>(routeList);
        // 将PageInfo<Route> 转成 PageInfo<RouteVo>
        PageInfo<RouteVo> resultPageInfo = new PageInfo<RouteVo>();
        BeanConv.toBean(pageInfo,resultPageInfo);
        // ★浅度拷贝(深度拷贝问题-->不能拷贝多层)
        resultPageInfo.setList(BeanConv.toBeanList(pageInfo.getList(),RouteVo.class));
        return resultPageInfo;
    }

    /**
     * 判断当前用户是否收藏当前线路
     */
    @Override
    public Boolean isFavorited(FavoriteVo favoriteVo) {
        UserVo userVo = (UserVo) session.getAttribute(session.getId());
        if (EmptyUtil.isNullOrEmpty(userVo)){
            // 当用户未登录时,按钮可以点击(false)
            return false;
        }
        Long userId = userVo.getId();
        FavoriteExample favoriteExample = new FavoriteExample();
        favoriteExample.createCriteria()
                .andUserIdEqualTo(userId)
                .andRouteIdEqualTo(favoriteVo.getRouteId());
        List<Favorite> favoriteList = favoriteMapper.selectByExample(favoriteExample);
        // true: 当前用户已收藏,按钮不可以点击
        // false: 当前用户未收藏当前线路,按钮可以点击
        return favoriteList.size()>0;
    }
}
